package com.ruoyi.eamp.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampClassDate;
import com.ruoyi.eamp.domain.EampClassManage;
import com.ruoyi.eamp.domain.vo.MangeAndDateVO;
import com.ruoyi.eamp.domain.vo.MangeAndListVO;
import com.ruoyi.eamp.domain.vo.UserAndDeptAndManageVO;
import com.ruoyi.eamp.mapper.EampClassDateMapper;
import com.ruoyi.eamp.mapper.EampClassManageMapper;
import com.ruoyi.eamp.service.IEampClassManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 排班值班关系表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class EampClassManageServiceImpl implements IEampClassManageService {
    @Autowired
    private EampClassManageMapper eampClassManageMapper;
    @Autowired
    private EampClassDateMapper eampClassDateMapper;

    /**
     * 查询排班值班关系表
     *
     * @param classId 排班值班关系表主键
     * @return 排班值班关系表
     */
    @Override
    public MangeAndDateVO selectEampClassManageByClassId(Long classId) {
        MangeAndDateVO mangeAndDateVO = eampClassManageMapper.selectEampClassManageByClassIdVO(classId);
        //取出所有中间表数据----14
        List<EampClassDate> eampClassDateList = mangeAndDateVO.getEampClassDateList();
        //拿出7组日期,7组人,用map保存日期---人的映射关系
        Map<Date, List<Long>> map = new TreeMap<>();
        for (EampClassDate eve : eampClassDateList) {
            Date date = eve.getDate();
            Long userId = eve.getDatePeople();
            List<Long> longs = map.get(date);
            if (longs == null) {
                List<Long> longList = new ArrayList<>();
                longList.add(userId);
                map.put(date, longList);
            } else {
                longs.add(userId);
            }
        }
        //到这里,就得到map,有7组数据
        // 给dateList,affactionList赋值
        List<Date> dateList=new ArrayList<>();
        List<List<Long>> affactionList=new ArrayList<>();
        Set<Map.Entry<Date, List<Long>>> entries = map.entrySet();
        for (Map.Entry<Date, List<Long>> eve:entries) {
            Date key = eve.getKey();
            List<Long> value = eve.getValue();
            dateList.add(key);
            affactionList.add(value);
        }
        //再设置这两个list到mangeAndDateVO中
        mangeAndDateVO.setDateList(dateList);
        mangeAndDateVO.setAffactionList(affactionList);
        return mangeAndDateVO;

        //     return eampClassManageMapper.selectEampClassManageByClassId(classId);
    }

    /**
     * 查询排班值班关系表列表
     *
     * @param eampClassManage 排班值班关系表
     * @return 排班值班关系表
     */
    @Override
    public List<EampClassManage> selectEampClassManageList(EampClassManage eampClassManage) {
        return eampClassManageMapper.selectEampClassManageList(eampClassManage);
    }

    /**
     * 新增排班值班关系表
     *
     * @param eampClassManage 排班值班关系表
     * @return 结果
     */
    @Override
    public int insertEampClassManage(EampClassManage eampClassManage) {
        return eampClassManageMapper.insertEampClassManage(eampClassManage);
    }

    /**
     * 修改排班值班关系表
     *
     * @param eampClassManage 排班值班关系表
     * @return 结果
     */
    @Override
    public int updateEampClassManage(EampClassManage eampClassManage) {
        return eampClassManageMapper.updateEampClassManage(eampClassManage);
    }

    /**
     * 批量删除排班值班关系表
     *
     * @param classIds 需要删除的排班值班关系表主键
     * @return 结果
     */
    @Override
    public int deleteEampClassManageByClassIds(Long[] classIds) {
        return eampClassManageMapper.deleteEampClassManageByClassIds(classIds);
    }

    /**
     * 删除排班值班关系表信息
     *
     * @param classId 排班值班关系表主键
     * @return 结果
     */
    @Override
    public int deleteEampClassManageByClassId(Long classId) {
        return eampClassManageMapper.deleteEampClassManageByClassId(classId);

    }

    @Override
    public List<SysDept> getDept() {
        return eampClassManageMapper.getDept();
    }

    @Override
    public List<UserAndDeptAndManageVO> getUsAndDe(UserAndDeptAndManageVO userAndDeptAndManageVO) {
        return eampClassManageMapper.getUsAndDe();
    }


    @Override
    public int applySend(MangeAndListVO mangeAndListVO) {
        List<Date> dateList = mangeAndListVO.getDateList();
        List<List<Long>> affactionList = mangeAndListVO.getAffactionList();
        Map<Date, List<Long>> map = new HashMap<>();
        for (int i = 0; i < dateList.size(); i++) {
            map.put(dateList.get(i), affactionList.get(i));

        }
        //判断是否连续日期,日期和人是否匹配
        if (dateList.size() != affactionList.size()) {
            return 0;
            //不连续返回错误
        }
        //判断人员和日期是否匹配
        if (!ContinuousDates(dateList)) {
            return 0;
            //不匹配返回错误代码
        }
        //新增排班
        EampClassManage eampClassManage = new EampClassManage();
        eampClassManage.setClassAffiliation(mangeAndListVO.getClassAffiliation());
        eampClassManage.setClassRemark(mangeAndListVO.getClassRemark());
        eampClassManage.setClassUrgent(mangeAndListVO.getClassUrgent());
        eampClassManage.setClassAnnex(mangeAndListVO.getClassAnnex());
        eampClassManage.setClassName(mangeAndListVO.getClassName());
        eampClassManage.setClassCreatePeople(SecurityUtils.getUsername());
        eampClassManage.setClassCreateTime(new Date());
        int i = eampClassManageMapper.insertEampClassManage(eampClassManage);
        //中间表新增
        List<EampClassDate> eampClassDateList = new ArrayList<>();
        for (Date eve : dateList) {
            List<Long> users = map.get(eve);
            for (Long every : users) {
                EampClassDate eampClassDate = new EampClassDate();
                eampClassDate.setClassId(eampClassManage.getClassId());
                eampClassDate.setDatePeople(every);
                eampClassDate.setDate(eve);
                eampClassDateList.add(eampClassDate);
            }
        }

        eampClassDateMapper.saveBatchManage(eampClassDateList);
        return i;
    }

    private boolean ContinuousDates(List<Date> dateList) {
        Collections.sort(dateList);
//        for (int i = 0; i < dateList.size(); i++) {
//            if (i == dateList.size() - 1) {
//                continue;
//            }
////            if (DateUtil.between(dateList.get(i), dateList.get(i + 1), DateUnit.DAY) != 1) {
////                //非连续日期
////                return false;
////            }
//        }
        //日期连续
        return true;

    }

}
